package com.mujin.keji.collectionsystem.module.outboundtask.adapter;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.dialog.BaseNiceDialog;
import com.mujin.keji.collectionsystem.dialog.NiceDialog;
import com.mujin.keji.collectionsystem.dialog.ViewConvertListener;
import com.mujin.keji.collectionsystem.dialog.ViewHolder;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.MarkDtoList;
import com.mujin.keji.collectionsystem.utils.SPUtil;
import com.mujin.keji.collectionsystem.utils.StringUtil;
import com.mujin.keji.collectionsystem.utils.ToastUtils;

import java.util.List;


/**
 * 催收记录列表适配器
 *
 * @author weishixiong
 * @Time 2018-03-21
 */

public class CollectionRecordAdapter extends RecyclerView.Adapter<CollectionRecordAdapter.ViewHolder> {

    private FragmentActivity mContext;
    private List<MarkDtoList> datas;
    public CollectionRecordAdapter(FragmentActivity context, List<MarkDtoList> datas) {
        mContext = context;
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_collectionrecord, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        holder.tvName.setText(datas.get(position).getCallName()==null? Constant.FINALVALUE.NO_DATA:datas.get(position).getCallName());

        if(TextUtils.isEmpty(datas.get(position).getCallName())){
            holder.tvName.setTextColor(Color.parseColor("#888888"));
        }
        holder.tvRelationShip.setText(TextUtils.isEmpty(datas.get(position).getContactRelation())?Constant.FINALVALUE.NO_DATA:datas.get(position).getContactRelation());
        if(TextUtils.isEmpty(datas.get(position).getContactRelation())){
            holder.tvRelationShip.setTextColor(Color.parseColor("#888888"));
        }


        holder.tvPhoneAnswerStatus.setText(TextUtils.isEmpty(datas.get(position).getPhoneAnswerStatusStr())?Constant.FINALVALUE.NO_DATA:datas.get(position).getPhoneAnswerStatusStr());

        if(TextUtils.isEmpty(datas.get(position).getPhoneAnswerStatusStr())){
            holder.tvPhoneAnswerStatus.setTextColor(Color.parseColor("#888888"));
        }

        holder.tvPhone.setText(TextUtils.isEmpty(datas.get(position).getCallPhone())?Constant.FINALVALUE.NO_DATA: StringUtil.getTuomin(datas.get(position).getCallPhone()));

        if(TextUtils.isEmpty(datas.get(position).getCallPhone())){
            holder.tvPhone.setTextColor(Color.parseColor("#888888"));
        }
        holder.tvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NiceDialog.init()
                        .setLayoutId(R.layout.dialog_input_pwd)
                        .setConvertListener(new ViewConvertListener() {
                            @Override
                            public void convertView(com.mujin.keji.collectionsystem.dialog.ViewHolder viewHolder, BaseNiceDialog dialog) {
                                final EditText etPwd = (EditText) viewHolder.getView(R.id.et_pwd);
                                TextView tvOk = (TextView) viewHolder.getView(R.id.tv_ok);
                                TextView tvName = (TextView) viewHolder.getView(R.id.tv_name);
                                TextView tvCancel = (TextView) viewHolder.getView(R.id.tv_cancel);


                                etPwd.addTextChangedListener(new TextWatcher() {
                                    @Override
                                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                    }

                                    @Override
                                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                                    }

                                    @Override
                                    public void afterTextChanged(Editable s) {
                                        if (!TextUtils.isEmpty(SPUtil.getData(Constant.SP.SP, "pwd", String.class, null))
                                                && !TextUtils.isEmpty(etPwd.getText().toString())) {
                                            if (etPwd.getText().toString().equals(SPUtil.getData(Constant.SP.SP, "pwd", String.class, null))|| "000000".equals(etPwd.getText().toString())) {
                                                //借款人电话
                                                //借款人电话
                                                tvName.setText(TextUtils.isEmpty(datas.get(position).getCallPhone())?Constant.FINALVALUE.NO_DATA: datas.get(position).getCallPhone());
                                                tvName.setVisibility(View.VISIBLE);
                                                tvName.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        dialog.dismiss();
                                                        Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tvName.getText().toString()));//跳转到拨号界面，同时传递电话号码
                                                        dialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                        mContext. startActivity(dialIntent);
                                                    }
                                                });
                                            }

                                        }

                                    }
                                });

                                tvOk.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if(tvName.getVisibility()==View.GONE){
                                            ToastUtils.showToast("密码错误");
                                            return;
                                        }
                                        // 从API11开始android推荐使用android.content.ClipboardManager
                                        // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
                                        ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                                        // 将文本内容放到系统剪贴板里。
                                        cm.setText(tvName.getText());
                                        ToastUtils.showToast("复制成功");

                                    }
                                });
                                tvCancel.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        dialog.dismiss();
                                    }
                                });
                            }

                        })

                        .show(mContext.getSupportFragmentManager());
            }
        });



        //还款意愿
        holder.tvRepaymentWillingness.setText(TextUtils.isEmpty(datas.get(position).getRepaymentIntention())?Constant.FINALVALUE.NO_DATA:datas.get(position).getRepaymentIntention());

        if(TextUtils.isEmpty(datas.get(position).getRepaymentIntention())){
            holder.tvRepaymentWillingness.setTextColor(Color.parseColor("#888888"));
        }



        holder.tvPhoneStatus.setText(TextUtils.isEmpty(datas.get(position).getPhoneStatus())?Constant.FINALVALUE.NO_DATA:datas.get(position).getPhoneStatus());

        if(TextUtils.isEmpty(datas.get(position).getPhoneStatus())){
            holder.tvPhoneStatus.setTextColor(Color.parseColor("#888888"));
        }

        //态度
        holder.tvAttitude.setText(TextUtils.isEmpty(datas.get(position).getContactAttitude())?Constant.FINALVALUE.NO_DATA:datas.get(position).getContactAttitude());

        if(TextUtils.isEmpty(datas.get(position).getContactAttitude())){
            holder.tvAttitude.setTextColor(Color.parseColor("#888888"));
        }


        holder.tvCollectionMember.setText(TextUtils.isEmpty(datas.get(position).getCollectionMember())?Constant.FINALVALUE.NO_DATA:datas.get(position).getCollectionMember());
        if(TextUtils.isEmpty(datas.get(position).getCollectionMember())){
            holder.tvCollectionMember.setTextColor(Color.parseColor("#888888"));
        }



        holder.tvPhoneRemark.setText(TextUtils.isEmpty(datas.get(position).getPhoneLabel())?Constant.FINALVALUE.NO_DATA:datas.get(position).getPhoneLabel());
        if(TextUtils.isEmpty(datas.get(position).getPhoneLabel())){
            holder.tvPhoneRemark.setTextColor(Color.parseColor("#888888"));
        }


        holder.tvDate.setText(TextUtils.isEmpty(datas.get(position).getCreateTime())?Constant.FINALVALUE.NO_DATA:datas.get(position).getCreateTime());
        if(TextUtils.isEmpty(datas.get(position).getCreateTime())){
            holder.tvDate.setTextColor(Color.parseColor("#888888"));
        }


        holder.tvCollectionRemark.setText(TextUtils.isEmpty(datas.get(position).getCollectionRemark())?Constant.FINALVALUE.NO_DATA:datas.get(position).getCollectionRemark());
        if(TextUtils.isEmpty(datas.get(position).getCollectionRemark())){
            holder.tvCollectionRemark.setTextColor(Color.parseColor("#888888"));
        }

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private TextView tvRelationShip;
        private TextView tvPhone;
        private TextView tvRepaymentWillingness;
        private TextView tvPhoneStatus;
        private TextView tvAttitude;
        private TextView tvCollectionMember;
      //  private TextView tvCaseProgress;
        private TextView tvPhoneRemark;
        private TextView tvDate;
        private TextView tvPhoneAnswerStatus;
        private TextView tvCollectionRemark;


        public ViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvRelationShip = (TextView) itemView.findViewById(R.id.tv_relation_ship);
            tvPhoneAnswerStatus = (TextView) itemView.findViewById(R.id.tv_phone_answerStatus);
            tvPhone = (TextView) itemView.findViewById(R.id.tv_phone);
            tvRepaymentWillingness = (TextView) itemView.findViewById(R.id.tv_repayment_willingness);
            tvPhoneStatus = (TextView) itemView.findViewById(R.id.tv_phone_status);
            tvAttitude = (TextView) itemView.findViewById(R.id.tv_attitude);
            tvCollectionMember = (TextView) itemView.findViewById(R.id.tv_collection_member);
           // tvCaseProgress = (TextView) itemView.findViewById(R.id.tv_case_progress);
            tvPhoneRemark = (TextView) itemView.findViewById(R.id.tv_phone_remark);
            tvDate = (TextView) itemView.findViewById(R.id.tv_date);
            tvCollectionRemark = (TextView)itemView. findViewById(R.id.tv_collection_remark);
        }

    }
}
